package person.bangbang.im.Androidgin;

import person.bangbang.im.Androidgin.R;
import person.bangbang.im.Androidgin.Framework.Message;
import person.bangbang.im.Androidgin.UI.ChatActivity;
import person.bangbang.im.Androidgin.UI.ContactList;
import person.bangbang.im.Androidgin.Util.Log;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

/**
 * a helper notify tools.
 *
 */
public class Notify {
	public static final int TYPE_MESSAGE = 1; //	Message notification.

	public static final int TYPE_EMAIL = 2; //	Single email notification.

	public static final int TYPE_EMAILS = 3; //	Multiple email notification.

	public static final int TYPE_FORMATTED = 4; //	Formatted text.

	public static final int TYPE_SEARCHRESULTS =5; //	Buddy search results.

	public static final int TYPE_USERINFO = 6; //	Formatted userinfo text.

	public static final int TYPE_URI = 7; //	URI notification or display. 
	
	public static final int SUBTYPE_MSG_ERROR = 0; //	Error notification.

	public static final int SUBTYPE_MSG_WARNING = 1; //	Warning notification.

	public static final int SUBTYPE_MSG_INFO =2; //	Information notification. 
	
	public void notifyEmail(){
		throw new RuntimeException("umimplemented..");
	}
	
	public void notifyMsg(String title, String primary, String secondary, int type, int notifyId) {
		throw new RuntimeException("umimplemented..");
	}
	public void notifyMsg(Message msg, int notifyId) {
   		int res = R.drawable.tray_new_im;
		String mFormatNotifyString = mCon.getResources().getString(R.string.notify_title_format);

		Notification n = new Notification(res, msg.getMessage(), System.currentTimeMillis());
		n.flags |= Notification.FLAG_AUTO_CANCEL;
		Intent fireActivity = new Intent(mCon, ChatActivity.class);
		fireActivity.putExtra(ContactList.CHAT_BUDDY_ID, msg.getFrom().getID());
		
		PendingIntent p = PendingIntent.getActivity(mCon, 
				                                    0, 
				                                    fireActivity, 
				                                    PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_ONE_SHOT);
		n.setLatestEventInfo(mCon, 
							String.format(mFormatNotifyString, msg.getFrom().getUserName()), 
							msg.getMessage(), 
							p
							);
		
		mNm.notify(notifyId, n);

	}

	private NotificationManager mNm;
	private static Notify _instance;
	private Context mCon;
	private Notify(Context con) {
		mCon = con;
		mNm = (NotificationManager) con.getSystemService(Context.NOTIFICATION_SERVICE);
	}
	public static Notify getInstance(Context con) {
		if (null == _instance) {
			_instance = new Notify(con);
		}
		
		return _instance;
	}
}
